package com.qidian.buy.service.impl;

import com.qidian.buy.entity.Customer;
import com.qidian.buy.entity.Orders;
import com.qidian.buy.mapper.OrdersMapper;
import com.qidian.buy.service.IOrdersService;
import com.qidian.buy.util.pojo.LayuiTableDTO;
import com.qidian.buy.util.pojo.QDResult;
import com.qidian.buy.util.vo.OrderSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author 郭浩
 */
@Service
public class OrdersServiceImpl implements IOrdersService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Transactional(propagation = Propagation.REQUIRED)
    public  void addOrder(Customer c, OrderSaveVo vo){
        // 产生订单编号，除过固定部分之外，其余部分从redis的使用incr获取\
        String orderId = "QD789456";// 固定部分
        // 读取非固定部分
        Long oid = null;
        if(redisTemplate.hasKey("ORDERID")){
            oid = redisTemplate.opsForValue().increment("ORDERID");
        }else{
            // 如果redis中没有初始的orderid，这里可以设置一个初始值
            oid = 9527L;
            redisTemplate.opsForValue().set("ORDERID",oid+"");
        }
        orderId += oid;
        //  保存订单信息。
        // 保存订单明细
        // 保存物流信息
        // 把对应的商品从购物车删除
    };
    @Resource
    private OrdersMapper ordersMapper;
    @Override
    public QDResult save(Orders orders) {
        if (ordersMapper.insert(orders)==1){
            return QDResult.ok();
        }
        return QDResult.fal();
    }

    @Override
    public QDResult edit(Orders orders) {
        if (ordersMapper.updateById(orders)==1){
            return QDResult.ok();
        }
        return QDResult.fal();
    }

    @Override
    public QDResult delete(Orders orders) {
        if (ordersMapper.updateById(orders)==1){
            return QDResult.ok();
        }
        return QDResult.fal();
    }

    @Override
    public QDResult<Orders> get(Long id) {
        Orders orders = ordersMapper.selectById(id);
        if (orders!=null){
            return QDResult.ok(orders);
        }
        return QDResult.notFound();
    }

    @Override
    public LayuiTableDTO<Orders> query(int page, int limit, Orders orders) {
        return null;
    }
}
